package com.example.rotateimageview.pendant

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.widget.LinearLayout
import androidx.core.view.marginEnd
import cn.neoclub.uki.commons.layout.*
import com.example.rotateimageview.R

class ShrinkView  @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var isShrinkState = false
    init {
        orientation = HORIZONTAL
        initView()
    }

    private fun initView() {
        val shrinkTargetView = ImageView {
            layout_width = 60
            layout_height = 124
            margin_end = 2
            src = R.drawable.bg_pendant
        }

        val clickShrinkView = ImageView {
             layout_width = 13
             layout_height = 60
             gravity = Gravity.CENTER
             layout_gravity =Gravity.CENTER_VERTICAL
             src = R.drawable.shrink_right
        }

        clickShrinkView.setOnClickListener {
            Log.i("---click","clickShrinkView")
            if(!isShrinkState){
                isShrinkState = true
                val translation = shrinkTargetView.width + clickShrinkView.width + shrinkTargetView.marginEnd
                shrinkTargetView.animate().translationX(translation.toFloat()).setDuration(300).start()
            }else{
                isShrinkState = false
                shrinkTargetView.animate().translationX(0f).setDuration(300).start()
            }
        }
    }
}